<template>
  <div>
    <el-dialog title="添加学习人员" :close-on-click-modal="false" :visible.sync="visible">
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;margin-top: 20px"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column align="center" type="index" header-align="center" label="ID"></el-table-column>
        <el-table-column prop="realname" header-align="center" align="center" label="姓名"></el-table-column>
        <el-table-column prop="mobile" header-align="center" align="center" label="手机号码"></el-table-column>
        <el-table-column prop="gender" header-align="center" align="center" label="性别">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.gender === 1">男</el-tag>
            <el-tag v-if="scope.row.gender === 2">女</el-tag>
          </template>
        </el-table-column>
        <!--        <el-table-column-->
        <!--          fixed="right"-->
        <!--          header-align="center"-->
        <!--          align="center"-->
        <!--          label="操作">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-button type="text" @click="examine(scope.row.id,scope.row.checkStatus)">审核-->
        <!--            </el-button>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
      </span>
    </el-dialog>
    <exam v-if="examVisible" ref="exam" @refreshDataList="getDataList"></exam>
  </div>
</template>

<script>
import Exam from './exam'

export default {
  data() {
    return {
      visible: false,
      dataListLoading: false,
      examVisible: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataList: [],
      records: [],
      dataListSelections: [],
      userIds: [],
      courseId: '',
      loading: false
    }
  },
  components: {
    Exam
  },
  methods: {
    getuserList() {
      this.$http({
        url: this.$http.adornUrl(
          `/train-admin/courseManage/getAllCourseUserIds`
        ),
        method: 'post',
        data: {
          courseId: this.courseId
        }
      }).then(res => {
        if (res && res.code === 200) {
          this.userIds = res.data
        } else {
          this.userIds = []
        }
        this.getDataList()
      })
    },
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(`/usercenter-admin/user/list`),
        method: 'post',
        data: {
          userIds: this.userIds.join(',')
        }
      }).then(res => {
        if (res && res.code === 200) {
          this.dataList = res.data.list
          this.totalPage = res.data.totalCount
        } else {
          this.dataList = []
        }
        this.dataListLoading = false
      })
    },
    init(id) {
      this.courseId = id
      this.visible = true
      this.getuserList()
    },
    dataFormSubmit() {
      for (var i in this.dataListSelections) {
        this.userIds.push(this.dataListSelections[i].userId)
        // this.userIds.push(i.userId)
      }
      this.$http({
        url: this.$http.adornUrl('/train-admin/courseManage/addCourseUser'),
        method: 'post',
        data: {
          courseId: this.courseId,
          userIds: this.userIds
        }
      }).then(data => {
        // if (data.code === 200) {
        //   this.data = data.data.list
        //   this.totalPage = data.data.totalCount
        //   this.userList = []
        //   this.userIds = []
        // } else {
        //   this.userList = []
        //   this.userIds = []
        //   this.$notify.error({
        //     title: '失败',
        //     message: '暂无人员列表'
        //   })
        // }
      })
    },
    examine(id, checkStatus) {
      this.examVisible = true
      this.$nextTick(() => {
        this.$refs.exam.init(id, checkStatus)
      })
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      console.log(val)
      this.dataListSelections = val
    }
  }
}
</script>

<style scoped>
</style>
